using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using Lexalytics;
using System.Runtime.InteropServices;

namespace SalienceEngineDemo
{
    static class Program
    {
        // defines for commandline output
        [DllImport("kernel32.dll")]
        static extern bool AttachConsole(int dwProcessId);
        private const int ATTACH_PARENT_PROCESS = -1;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            // redirect console output to parent process;
            // must be before any calls to Console.WriteLine()
            AttachConsole(ATTACH_PARENT_PROCESS);

            if (args.Length == 1)
            {
                if (args[0].Equals("-h") || args[0].Equals("-help"))
                {
                    string HelpInfo = "\nThe following are the commands to successfully switch your data directory:\n\n";
                    HelpInfo += "\tTo change the data directory for your current/active settings, please perform:\n";
                    HelpInfo += "\t\tSalienceDemo.exe <data-directory path>\n";
                    HelpInfo += "\tTo change the data directory for settings of your choice, please perform:\n";
                    HelpInfo += "\t\tSalienceDemo.exe <settings name> <data-directory path>\n\n";
                    HelpInfo += "Note: Your settings name is the same as the folder name for your data directory.";
                    Console.Write(HelpInfo);
                    
                }
                else
                {
                    if (Directory.Exists(args[0]))
                    {
                        Configuration config = new Configuration();
                        config.activeSettings().settings["DataDirectory"] = args[0];
                        config.activeSettings().settings["UserDirectory"] = args[0] + "\\user";
                        config.activeSettings().settings["DefaultQueryTopicFile"] = args[0] + "\\salience\\tags\\sampletags.dat";
                        config.activeSettings().settings["DefaultConceptTopicFile"] = args[0] + "\\salience\\concepts\\example.dat";
                        if (File.Exists(args[0] + "\\salience\\sentiment\\general.hsdx"))
                            config.activeSettings().settings["DefaultHSDFile"] = args[0] + "\\salience\\sentiment\\general.hsdx";
                        else
                            config.activeSettings().settings["DefaultHSDFile"] = args[0] + "\\salience\\sentiment\\general.hsd";
                        config.activeSettings().Save();
                        Console.WriteLine("The data directory for settings \""+config.activeSettings().settings["Name"]+"\" has successfully changed.");
                    }
                    else
                    {
                        Console.WriteLine("The data directory you have entered (\"" + args[0] + "\") could not be found. Please try again.");
                    }
                }
            }
            else if (args.Length == 2)
            {
                if (Directory.Exists(args[1]))
                {
                    Configuration config = new Configuration();
                    bool bFound = false;
                    foreach (Settings settings in config.userSettings)
                    {
                        if (settings.settings["Name"].Equals(args[0]))
                        {
                            settings.settings["DataDirectory"] = args[1];
                            settings.settings["UserDirectory"] = args[1] + "\\user";
                            settings.settings["DefaultQueryTopicFile"] = args[1] + "\\salience\\tags\\sampletags.dat";
                            settings.settings["DefaultConceptTopicFile"] = args[1] + "\\salience\\concepts\\example.dat";
                            if (File.Exists(args[1] + "\\salience\\sentiment\\general.hsdx"))
                                settings.settings["DefaultHSDFile"] = args[1] + "\\salience\\sentiment\\general.hsdx";
                            else
                                settings.settings["DefaultHSDFile"] = args[1] + "\\salience\\sentiment\\general.hsd";
                            settings.Save();
                            Console.WriteLine("The data directory for settings \"" + args[0] + "\" has successfully changed.");
                            bFound = true;
                            break;
                        }
                    }
                    if (!bFound)
                    {
                        Console.WriteLine("The settings name you have entered (" + args[0] + ") could not be found. Please try again.");
                    }
                }
                else
                {
                    Console.WriteLine("The data directory you have entered (" + args[1] + ") could not be found. Please try again.");
                }
            }
                    
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            try
            {
                Application.Run(new SalienceDemo());
            }
            catch (BadImageFormatException errBadImage)
            {
                MessageBox.Show(errBadImage.FileName + ": " + errBadImage.Message, "Bad Image exception");
                Application.Exit();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
                Application.Exit();
            }
        }

    
    }
}